var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all) __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if ((from && typeof from === 'object') || typeof from === 'function') {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, {
          get: () => from[key],
          enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable,
        });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (
  (target = mod != null ? __create(__getProtoOf(mod)) : {}),
  __copyProps(
    // If the importer is in node compatibility mode or this is not an ESM
    // file that has been converted to a CommonJS file using a Babel-
    // compatible transform (i.e. "__esModule" has not been set), then set
    // "default" to the CommonJS "module.exports" for node compatibility.
    isNodeMode || !mod || !mod.__esModule
      ? __defProp(target, 'default', { value: mod, enumerable: true })
      : target,
    mod,
  )
);
var __toCommonJS = (mod) => __copyProps(__defProp({}, '__esModule', { value: true }), mod);

// src/DatePicker/CalendarPanel/index.tsx
var CalendarPanel_exports = {};
__export(CalendarPanel_exports, {
  default: () => CalendarPanel_default,
});
module.exports = __toCommonJS(CalendarPanel_exports);
var import_LeftOutlined = __toESM(require('@ant-design/icons/LeftOutlined'));
var import_RightOutlined = __toESM(require('@ant-design/icons/RightOutlined'));
var import_antd = require('antd');
var import_classnames = __toESM(require('classnames'));
var import_dayjs = __toESM(require('dayjs'));
var import_localeData = __toESM(require('dayjs/plugin/localeData'));
var import_weekday = __toESM(require('dayjs/plugin/weekday'));
var import_react = __toESM(require('react'));
var import_createField = __toESM(require('../../createField'));
var import_hooks = require('@szzj/hooks');
var import_index = require('./index.less');
var CLS_PREFIX = 'szzj-calendar-panel';
import_dayjs.default.extend(import_weekday.default);
import_dayjs.default.locale('zh-cn');
import_dayjs.default.extend(import_localeData.default);
function CalendarPanel({ value, onChange, onPanelChange, scheduledDates, headerRender, ...rest }) {
  const [innerValue, setInnerValue] = (0, import_react.useState)(
    value ?? (0, import_dayjs.default)(),
  );
  (0, import_react.useEffect)(() => {
    const dateFromValue = value ?? (0, import_dayjs.default)();
    if (!(innerValue == null ? void 0 : innerValue.isSame(dateFromValue, 'day')))
      setInnerValue(value);
  }, [value]);
  const getDateRange = (0, import_react.useCallback)((date) => {
    const weekday2 = date.weekday();
    const monthStartDate = date.date(1);
    let startDate = monthStartDate == null ? void 0 : monthStartDate.add(-weekday2, 'day');
    if (startDate.month() === date.month() && startDate.day() !== 1) {
      startDate = startDate == null ? void 0 : startDate.add(-7, 'day');
    }
    const endDate = monthStartDate == null ? void 0 : monthStartDate.add(-weekday2 + 41, 'day');
    return [startDate, endDate];
  }, []);
  (0, import_hooks.useMount)(() => {
    if (innerValue) {
      const [startDate, endDate] = getDateRange(innerValue);
      onPanelChange == null ? void 0 : onPanelChange(startDate, endDate);
      onChange == null ? void 0 : onChange(innerValue);
    }
  });
  const triggerDateChange = (0, import_react.useCallback)(
    (date) => {
      setInnerValue(date);
      onChange == null ? void 0 : onChange(date);
    },
    [onChange],
  );
  const triggerMonthChange = (0, import_react.useCallback)(
    (month) => {
      const date =
        innerValue == null ? void 0 : innerValue.clone().year(month.year()).month(month.month());
      if (date) {
        triggerDateChange(date);
        const [startDate, endDate] = getDateRange(date);
        onPanelChange == null ? void 0 : onPanelChange(startDate, endDate);
      }
    },
    [onPanelChange, triggerDateChange, getDateRange],
  );
  const innerHeaderRender = (0, import_react.useCallback)(
    ({ value: value2 }) => {
      const createMonthChanger = (offset) => {
        return () => {
          const month = value2.clone().month(value2.month() + offset);
          triggerMonthChange(month);
        };
      };
      return /* @__PURE__ */ import_react.default.createElement(
        'div',
        { className: `${CLS_PREFIX}-header` },
        /* @__PURE__ */ import_react.default.createElement('div', null, value2.format('YYYY年M月')),
        /* @__PURE__ */ import_react.default.createElement(
          'div',
          null,
          /* @__PURE__ */ import_react.default.createElement(import_LeftOutlined.default, {
            style: { marginRight: '20px' },
            onClick: createMonthChanger(-1),
          }),
          /* @__PURE__ */ import_react.default.createElement(import_RightOutlined.default, {
            onClick: createMonthChanger(1),
          }),
        ),
      );
    },
    [triggerMonthChange],
  );
  const finalHeaderRender = headerRender || innerHeaderRender;
  const fullCellRender = (0, import_react.useCallback)(
    (date) => {
      const isToday = date == null ? void 0 : date.isSame((0, import_dayjs.default)(), 'day');
      const isAvtived = date == null ? void 0 : date.isSame(innerValue, 'day');
      const isScheduled =
        scheduledDates == null ? void 0 : scheduledDates.some((it) => it.isSame(date, 'day'));
      const cellClsPrefix = `${CLS_PREFIX}-date-cell`;
      const cellCalssName = (0, import_classnames.default)({
        [cellClsPrefix]: true,
        [`${cellClsPrefix}-actived`]: isAvtived,
      });
      return /* @__PURE__ */ import_react.default.createElement(
        'div',
        { className: `${cellClsPrefix}-wrapper` },
        /* @__PURE__ */ import_react.default.createElement(
          'div',
          { className: cellCalssName },
          isToday ? '今' : date.format('D'),
        ),
        isScheduled &&
          /* @__PURE__ */ import_react.default.createElement('i', {
            className: `${cellClsPrefix}-scheduled`,
          }),
      );
    },
    [innerValue, scheduledDates],
  );
  return /* @__PURE__ */ import_react.default.createElement(import_antd.Calendar, {
    className: CLS_PREFIX,
    fullscreen: false,
    ...rest,
    value,
    onSelect: triggerDateChange,
    onPanelChange: triggerMonthChange,
    headerRender: finalHeaderRender,
    dateFullCellRender: fullCellRender,
    fullCellRender,
  });
}
var CalendarPanel_default = (0, import_createField.default)(CalendarPanel, {
  defaultDetailComponent: ({ value }) => {
    return /* @__PURE__ */ import_react.default.createElement(import_antd.Calendar, {
      className: CLS_PREFIX,
      fullscreen: false,
      value,
      onSelect: () => {},
      onChange: () => {},
      onPanelChange: () => {},
      headerRender: () =>
        /* @__PURE__ */ import_react.default.createElement('div', null, value.format('YYYY/MM/DD')),
    });
  },
});
